
import UIKit
import JXSegmentedView

class NavigationInterface: UINavigationController, UIGestureRecognizerDelegate, UINavigationControllerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationBar.shadowImage = UIImage()
        delegate = self
        interactivePopGestureRecognizer?.isEnabled = true
        interactivePopGestureRecognizer?.delegate = self
        let shadow = NSShadow()
        shadow.shadowBlurRadius = 0
        shadow.shadowOffset = CGSize(width: 0, height: 0)
        shadow.shadowColor = UIColor.black.withAlphaComponent(0)
        
        if #available(iOS 13.0, *) {
            
            let appearance = UINavigationBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = .clear
            appearance.shadowColor = .clear
            appearance.titleTextAttributes = [NSAttributedString.Key.font: UIFont.setTypeFace(20.Fit, type: .bold), NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.shadow: shadow]
            navigationBar.standardAppearance = appearance
            navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
            
        }  else {
            navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont.setTypeFace(20.Fit, type: .bold), NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.shadow: shadow]
        }
            
    }
            

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        var controllerIDs = [String]()
        viewControllers.forEach { item in
            if let controller = item as? BasicViewController {
                controllerIDs.append(controller.ant_identification())
            }
        }
        let currentController = viewController as? BasicViewController
        if let controllerID = controllerIDs.filter({ $0 == currentController?.ant_identification() }).first {
            for item in viewControllers {
                if let controller = item as? BasicViewController, controller.ant_identification() == controllerID {
                    popToViewController(item, animated: true)
                }
            }
            return
        }
        if viewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage.init(named: "icon_return")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(popViewController(animated:)))
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    override var childForStatusBarStyle: UIViewController? {
        return topViewController
    }
    
    override func popViewController(animated: Bool) -> UIViewController? {
        super.popViewController(animated: true)
        return self.topViewController
    }
    
    override func popToRootViewController(animated: Bool) -> [UIViewController]? {
        if animated {
            let popController = self.viewControllers.last
            popController?.hidesBottomBarWhenPushed = false
        }
        return super.popToRootViewController(animated: animated)
    }
    
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        let isHidden = viewController.conforms(to: NaviBarHidden.self)
        viewController.navigationController?.setNavigationBarHidden(isHidden, animated: true)
        let isDisablePopGesture = viewController.conforms(to: DisablePopGestureDelegate.self)
        interactivePopGestureRecognizer?.isEnabled = !isDisablePopGesture
    }
    
}


extension NavigationInterface: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}

@objc protocol NaviBarHidden {}
@objc protocol DisablePopGestureDelegate {}
